import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';//svg

import { viteMockServe } from 'vite-plugin-mock' // mock

import path from "path"

// command：运行环境
export default defineConfig(({ command }) => {
  return {
    server: {
      port: '8090',
      host: 'localhost',
      // 代理
      proxy: {
        '/ai': {
          target: 'http://192.168.9.52:11434',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/ai/, '')
        },
        '/aa': {
          target: 'https://api.coze.cn',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/aa/, '')
        }
      },
    },
    plugins: [
      vue(),
      vueJsx(),
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        // 指定symbolId格式
        symbolId: 'icon-[dir]-[name]',
      }),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
      // mock
      viteMockServe({
        mockPath: './mock',
        localEnabled: command === 'serve',//开发阶段可以使用mock
      }),
    ],
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src")
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "@/assets/style/constant.scss";'//配置scss全局变量，在其他页面的css中使用，如：$color
        }
      }
    },
    build: {
      rollupOptions: {
        manualChunks(id) {
          // 如果是第三方库，则全部打包到vendor
          if (id.includes('node_modules')) {
            return "vendor"
          }
          console.log("====", id)
        }
      },
      chunkSizeWarningLimit: 2048,//打包大小限制
    }
  }
})

// https://vitejs.dev/config/

